<?php include HEADER; ?>

<link href="<?php echo __URL_BASE ?>css/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="<?php echo __URL_BASE ?>js/jquery-ui.js" type="text/javascript"></script>

<style>
.slg{
	width: 50px !important;
	padding: 2px !important;
	position: relative;
	top: 1px;
	left: 5px;
}
</style>
<?php
	$hoadon_id = (isset($hoadon)) ? $hoadon->id : 0;
	$mahoadon = (isset($hoadon)) ? $hoadon->MaHoaDon : '';
	$tendangnhap = (isset($hoadon)) ? $hoadon->TenDangNhap : ''; //echo $tendangnhap; die;
	$idphuongthuc = (isset($hoadon)) ? $hoadon->idPhuongThucThanhToan : '';
	$ngaydathang = (isset($hoadon)) ? date('d/m/Y',strtotime($hoadon->NgayDatHang)) : date('d/m/Y');
	$ten = (isset($hoadon)) ? $hoadon->TenKH : '';
	$email = (isset($hoadon)) ? $hoadon->EmailKH : '';
	$dienthoai = (isset($hoadon)) ? $hoadon->DienThoaiKH : '';
	$diachi = (isset($hoadon)) ? $hoadon->DiaChiKH : '';
	
	$userModel = new user();
	$user = $userModel->getUserByTenDangNhap($tendangnhap);
	
	if($idphuongthuc!=''){
		$phuongthucModel = new phuongthucthanhtoan();
		$phuongthuc = $phuongthucModel->getPhuongThucThanhToanById($idphuongthuc);
	}
	
	//get string <option> tivi
	$strTivi = "<option value=''></option>";
	$tiviModel = new tivi();
	$lstTivi = $tiviModel->getTiviByWhere('t.TrangThai=1','');
	if(count($lstTivi)>0){
		foreach($lstTivi as $item){
			$strTivi .= "<option value='".$item->id."'>".$item->TenTivi."</option>";
		}
	}
	
	//get string <option> user
	$strUser = "<option value=''></option>";
	$userModel = new user();
	$lstUser = $userModel->getUserByWhere('TrangThai=1 && Quyen <> 0','');
	if(count($lstUser)>0){
		foreach($lstUser as $item){
			$strUser .= "<option value='".$item->TenDangNhap."'>".$item->TenDangNhap."</option>";
		}
	}
?>

<div >
<form method="post" action="<?php if(isset($_REQUEST['hoadon_id'])) 
									echo __URL_BASE.'index.php?rt=admin_order/save&hoadon_id='.$hoadon_id ;
								  else	
									echo __URL_BASE.'index.php?rt=admin_order/save';
							?>" >

    <table class="TableUpdate" border="1">
    
        <tr>
            <td class="Control" colspan="2">
              <ul>
                <li>
                   <button type="submit" class="uupdate" style="border:none" >Ghi lại</button>
                </li>
                <li>
                  <label class="uback"  >Trở về</label>
                </li>
              </ul></td>
          </tr>
		  <tr <?php if(!isset($_REQUEST['hoadon_id'])) echo "style='display:none;'"; ?>>
            <th>Mã hóa đơn :</th>
            <td><input type="text"  class="text" name="tmahoadon" disabled="disabled" value="<?php echo $mahoadon ?>" /></td>
          </tr>
          <tr>
            <th>Tài khoản đặt hàng :</th>
            <td>
			<?php if(isset($_REQUEST['hoadon_id'])) : ?>
				<input type="text"  class="text" id="tusername" name="tusername" <?php if(isset($_REQUEST['hoadon_id'])) echo 'disabled="disabled"'; else echo 'readonly'; ?> value="<?php echo $tendangnhap ?>" />
			<?php else : ?>
				<select data-placeholder="Chọn một khách hàng..." class="chosen-select" name="stendangnhap" style="width: 412px;" tabindex="-1">
					<?php echo $strUser; ?>
				</select>
			<?php endif; ?>
			</td>
          </tr>
		  <tr>
            <th>Phương thức thanh toán :</th>
            <td><?php if(isset($_REQUEST['hoadon_id'])) : ?>
					<input type="text"  class="text" name="tphuongthuc" disabled="disabled" value="<?php echo $phuongthuc->MoTa ?>" />
				<?php else : ?>
					<select data-placeholder="Chọn một phương thức thanh toán..." class="chosen-select" name="sphuongthuc" style="width: 412px;" tabindex="-1">
						<option value=""></option>
				<?php
						foreach($lstpt as $item) :
							echo '<option value="'.$item->id.'">'.$item->MoTa.'</option>';
						endforeach;
				?>
					</select>
				<?php endif; ?>
			</td>
          </tr>
		  <tr>
            <th>Ngày đặt hàng :</th>
            <td><input type="text"  class="text" name="tngaydat" <?php if(isset($_REQUEST['hoadon_id'])) echo 'disabled="disabled"'; else echo 'readonly'; ?> value="<?php echo $ngaydathang;  ?>" /></td>
          </tr>
          <tr>
            <th>Sản phẩm đặt :</th>
            <td>
			<?php if(!isset($_REQUEST['hoadon_id'])) : ?>
					<div id="group">
						
						<input type="text" id="tivi1" class="tivi text" readonly value="" required/>
						
						<input type="number" class="text num slg tivi1"  value="1" />
					</div>
					<div id="products" title="Chọn sách">
                        <p>
                        
                        </p>
                    </div>
					<img id="btadd" style="cursor:pointer;margin-top:3px;" src="<?php echo __URL_BASE ?>img/add.png">
					
			<?php else : ?>
				<?php $hoadonModel = new hoadon();
					  $chitiethd = $hoadonModel->getChiTietHoaDonByIdHoaDon($hoadon->id);
					  foreach($chitiethd as $item){
						
						$tivi = $tiviModel->getTiviById($item->idTivi);
				?>
						<p><input type="text"  class="text" disabled="disabled" value="<?php echo $tivi->TenTivi ?>" />
						<input type="number" class="text num slg tivi1" disabled="disabled"  value="<?php echo $item->SoLuong ?>" /></p>
						
				<?php
					  }
				?>
			<?php endif; ?>
			</td>
          </tr>
		  <?php if(isset($_REQUEST['hoadon_id'])) :?>
		   <tr>
            <th>Trạng thái :</th>
            <td>
				<?php if($hoadon->TrangThai==4 || $hoadon->TrangThai==3) : ?>
						<input type="text"  class="text" name="ttrangthai" disabled="disabled" value="<?php echo utility::getOrderStatus($hoadon->TrangThai) ?>" />
				<?php endif; ?>
						<select <?php if($hoadon->TrangThai!=4 && $hoadon->TrangThai!=3) echo'class="chosen-select"'; ?> name="strangthai" style="width:412px;<?php if($hoadon->TrangThai==4 || $hoadon->TrangThai==3) echo 'display:none'; ?>" >
							<option value="0" <?php if($hoadon->TrangThai==0) echo 'selected="selected"'; ?>>Chưa xác nhận</option>
							<option value="1" <?php if($hoadon->TrangThai==1) echo 'selected="selected"'; ?>>Đã xác nhận</option>
							<option value="2" <?php if($hoadon->TrangThai==2) echo 'selected="selected"'; ?>>Đã thanh toán</option>
							<option value="3" <?php if($hoadon->TrangThai==3) echo 'selected="selected"'; ?>>Hủy bởi khách hàng</option>
							<option value="4" <?php if($hoadon->TrangThai==4) echo 'selected="selected"'; ?>>Hủy bởi Shoptivi</option>
						</select>
				
			</td>
          </tr>
		  <?php endif; ?>
		  <tr>
			  <td colspan="2" style="border-left:none;border-right:none;">
				<div class="order_fpage_row_box" style=" margin-right:20px;">Địa chỉ giao hàng</div>
			  </td>
		  </tr>
          <tr>
            <th>Tên người nhận :</th>
            <td><input type="text" id="tname" class="text" name="tname" value="<?php echo $ten ?>" <?php if(isset($hoadon) && ($hoadon->TrangThai == 4 || $hoadon->TrangThai == 3)) echo 'disabled="disabled"'; ?> required/></td>
          </tr>
          
          <tr>
            <th>Email người nhận:</th>
            <td><input type="text" id="tphangiai" class="text" name="temail" value="<?php echo $email ?>" <?php if(isset($hoadon) && ($hoadon->TrangThai == 4 || $hoadon->TrangThai == 3)) echo 'disabled="disabled"'; ?> required/></td>
          </tr>
          <tr>
            <th>Điện thoại người nhận:</th>
            <td><input type="text" id="tkichthuoc" class="text num" style="width:400px;" name="tphone" value="<?php echo $dienthoai ?>" <?php if(isset($hoadon) && ($hoadon->TrangThai == 4 || $hoadon->TrangThai == 3)) echo 'disabled="disabled"'; ?> required/></td>
          </tr>
          <tr>
            <th>Địa chỉ người nhận:</th>
            <td><input id="tgiathat" type="text"  name="taddress" class="text" value="<?php echo $diachi ?>" <?php if(isset($hoadon) && ($hoadon->TrangThai == 4 || $hoadon->TrangThai == 3)) echo 'disabled="disabled"'; ?> required/></td>
          </tr>
          
		  
          <tr>
            <td class="Control" colspan="2">
              <ul>
                <li>
                  <button type="submit" class="uupdate" style="border:none" >Ghi lại</button>
                </li>
                <li>
                  <label class="uback"  >Trở về</label>
                </li>
              </ul></td>
          </tr>
    </table>
</form>

 </div>
<?php include FOOTER; ?>

<script>
$(function() {
	$( "#products" ).dialog({
		autoOpen: false,
		show: {
			effect: "blind",
			duration: 1000
		},
		hide: {
			effect: "explode",
			duration: 1000
		}
	});

	$(document).on("click", "input[id^=tivi]" ,function() {
		debugger;
		book = $(this).attr("id");
		$.ajax({
			url: '<?php echo __URL_BASE?>index.php?rt=admin_order/load_tivi',
			type: 'post',
			datatype: 'html',
			data: {book: book},
			success: (function (data) {
				$('#products').html(data);
				$( "#products" ).dialog( "open" );
			})
		});
		
	});
});
$(document).on("click",".uupdate",function(){
	//check bắt phải chọn 1 tài khoản để đặt hàng
	if($("select[name=stendangnhap]").val() == ''){
		alert('Chọn một tài khoản để đặt hàng');
		return false;
	}
	//check phải chọn 1 phương thức thanh toán
	if($("select[name=sphuongthuc]").val() == ''){
		alert('Chọn một phương thức thanh toán');
		return false;
	}
	flag = false;
	$("#group .tivi").each(function(){
		if($(this).val() == ''){
			flag = true;
			return false;
		}
	});
	if(flag == true){
		alert('Phần lựa chọn sách không được để trống');
		return false;
	}	
});
$(document).on("click","#btadd",function(){
	debugger;
	
	var i = 1;
	$("#group .tivi").each(function(){
			i++;
	});
	$("#group").append("<br title='del"+ i +"' />");
	$("#group").append("<input type='text' id='tivi"+i+"' title='del"+i+"' class='tivi text' value=''/>");
	$("#group").append('<input type="number" class="text num slg tivi'+i+'"  title="del'+i+'" style="margin-left:4px;" /> ');
	$("#group").append("<img src='<?php echo __URL_BASE ?>img/DeleteRed.png' title='del"+ i +"' id='del"+ i +"'  style='width:20px;cursor:pointer;position:relative;top:5px;margin-left:10px;' />");
});
$(document).on("click","#group [id^='del']",function(){
	//debugger;
	var title = jQuery(this).prop("title").toLowerCase();
	$("#group [title='"+title+"']").remove();
});


<?php 
	if(isset($_REQUEST['count_err']) && $_REQUEST['count_err'] == 'true'){
?>
		alert('Một hoặc nhiều tivi không đáp ứng được số lượng. Mời bạn kiểm tra lại.\n Cập nhật trạng thái thất bại.');
<?php		
	}
?>
</script>

  
